package com.school.chapter01.homework;


@SuppressWarnings({"all"})
public class StudentsArray<E> {
    private E[] data;
    private int size;

    public StudentsArray(int capacity) {
        data = (E[]) new Object[capacity];//下转型
        size = 0;
    }

    public StudentsArray() {
        this(10);
    }

    public int getCapacity() {
        return data.length;
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    //在数组末尾添加
    public void addLast(E e) {
        add(size, e);
    }

    //在数组开头
    public void addFirst(E e) {
        add(0, e);
    }

    //在指定索引处插入
    public void add(int index, E e) {
        if (size == data.length) {
            throw new IllegalArgumentException("无法添加，数组已满");
        }
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("数组越界1");
        }
        for (int i = size - 1; i > index; i--) {
            data[i + 1] = data[i];
        }
        data[index] = e;
        size++;
    }

    //通过索引删除数据
    public E deleteByIndex(int index) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("数组越界2");
        }
        E cur = data[index];
        for (int i = index + 1; i < size; i++) {
            data[i - 1] = data[i];
        }
        size--;
        return cur;
    }

    public String toString() {
//链表为空链表
        if (isEmpty()) {
            return "[]";
        } else {
            StringBuilder res = new StringBuilder();
            res.append(String.format("Array:size=%d, capacity=%d\n", size, data.length));
            res.append('[');
            for (int i = 0; i < size; i++) {
                res.append(data[i]);
                if (i != size - 1) {
                    res.append(",");
                }
            }
            res.append(']');
            return res.toString();
        }

    }

}

class Students {
    private int id;
    private String name;
    private int age;
    private String sex;
    private String tel;

    public Students(int id, String name, int age, String sex, String tel) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.tel = tel;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    @Override
    public String toString() {
        return "Students{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", tel='" + tel + '\'' +
                '}' + "\n";
    }
}